<?php
class Ciucourse extends AppModel {
	var $name = 'Ciucourse';
	var $validate = array(
		'ciulanguage_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'ciulevel_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'ciutimetable_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'ciuroom_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'ciuuser_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'message' => 'Favor escoger un docente',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'ciucoursestate_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'ciuacademicperiod_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'Ciulanguage' => array(
			'className' => 'Ciulanguage',
			'foreignKey' => 'ciulanguage_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Ciulevel' => array(
			'className' => 'Ciulevel',
			'foreignKey' => 'ciulevel_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Ciutimetable' => array(
			'className' => 'Ciutimetable',
			'foreignKey' => 'ciutimetable_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Ciuroom' => array(
			'className' => 'Ciuroom',
			'foreignKey' => 'ciuroom_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Ciuuser' => array(
			'className' => 'Ciuuser',
			'foreignKey' => 'ciuuser_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Ciucoursestate' => array(
			'className' => 'Ciucoursestate',
			'foreignKey' => 'ciucoursestate_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Ciuacademicperiod' => array(
			'className' => 'Ciuacademicperiod',
			'foreignKey' => 'ciuacademicperiod_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Ciusubject' => array(
			'className' => 'Ciusubject',
			'foreignKey' => 'ciusubject_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	var $hasMany = array(
		'CiucourseCiustudent' => array(
			'className' => 'CiucourseCiustudent',
			'foreignKey' => 'ciucourse_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

	/**
	 * Revisa que no exista otro curso del mismo lenguaje, nivel, horaario y periodo
	 * @param unknown_type $newRec
	 * @return boolean
	 */
	function isDuplicated( $newRec ){
		$result=true;
		$sql="select count(*) as qty from ciucourses where "
		." ciuroom_id=". $newRec['ciuroom_id']
		." and ciusubject_id=". $newRec['ciusubject_id']
		." and ciutimetable_id=". $newRec['ciutimetable_id']
		." and ciuacademicperiod_id=". $newRec['ciuacademicperiod_id']
		;
	    $queryresult=$this->query($sql);
		$qty=$queryresult[0][0]['qty']; 	
		if($qty<1){
			$result=false;
		}
		return $result;		
	}
	
	/**
	 * Permite borrar solo si no tiene estudiantes inscritos
	 * @param unknown_type $id
	 * @return boolean
	 */
	function isOkToErase($id){
		$result=false;
		$sql="select count(*) as qty from ciucourse_ciustudents where "
		." ciucourse_id=". $id ;
	    $queryresult=$this->query($sql);
		$qty=$queryresult[0][0]['qty']; 	
		if($qty<1){
			$result=true;
		}
		return $result;
	}
	
}
